//
//  STLogViewController.swift
//  iPhone
//
//  Created by Toj on 8/28/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STLogViewController: STBaseViewController {
    
    private lazy var writeButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitleColor(.random, for: .normal)
        view.setTitle("打印日志", for: .normal)
        view.addTarget(self, action: #selector(didTapWrite), for: .touchUpInside)
        return view
    }()
    
    private lazy var writeListButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = .random
        view.setTitleColor(.random, for: .normal)
        view.setTitle("打印多条日志", for: .normal)
        view.addTarget(self, action: #selector(didTapWriteList), for: .touchUpInside)
        return view
    }()
    
    /** <#NOTE#> **/
    private lazy var logView: STLogView = {
        let view = STLogView()
        return view
    }()
    
    deinit {
        print("-->toj: \(cm_className) func: deinit")
    }
}

// MARK: - 生命周期
extension STLogViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件/ 通知事件回调
@objc private extension STLogViewController {
    
   func didTapWrite() {
       let random = arc4random_uniform(2345678) + 34567
       let log = "index: 0, log: " + random.to_string
       logView.log(add: log)
       logView.isHidden = false
    }
    
    
   func didTapWriteList() {
       var logs = [String]()
       let random = arc4random_uniform(50) + 10
       var log = ""
       for i in 0..<random {
           log = "index: \(i), log: " + (arc4random_uniform(2345678) + 34567).to_string
           logs.append(log)
       }
       
       logView.log(add: logs)
       logView.isHidden = false
    }
}

// MARK: - 初始化设置
private extension STLogViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(writeButton)
        view.addSubview(writeListButton)
        view.addSubview(logView)
    }
    
    func setupMakeLayoutSubviews() {
        let cls = 2
        let _fcls = cls.to_cgfloat
        let spacing = 50.to_cgfloat
        let width = 150.to_cgfloat
        var y = CGFloat.nav_barFullHeight
        var x = (view.frame.width - width * _fcls - (_fcls - 1) * spacing) * 0.5
        writeButton.snp.makeConstraints { make in
            make.top.equalTo(y)
            make.left.equalTo(x)
            make.width.equalTo(150)
            make.height.equalTo(44)
        }
        
        x += (spacing + width)
        writeListButton.snp.makeConstraints { make in
            make.left.equalTo(x)
            make.top.width.height.equalTo(writeButton)
        }
        
        y += 44
        logView.snp.makeConstraints { make in
            make.top.equalTo(y)
            make.left.bottom.right.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = .random
    }
}
